package com.fenghuajueli.libbasecoreui.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.fenghuajueli.libbasecoreui.R;
import com.fenghuajueli.libbasecoreui.mvp.BaseActivity;
import com.fenghuajueli.libbasecoreui.utils.PrivacyConstantsUtils;
import com.fenghuajueli.libbasecoreui.widget.MyActionBar;
import com.fenghuajueli.libbasecoreui.widget.ProgressWebview;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class PrivacyWebActivity extends BaseActivity {

    private static final String TYPE = "type";

    private ProgressWebview webView;
    private MyActionBar myActionBar;
    private String currentType = "";
    /**
     * 隐私政策默认的协议连接
     */
    private static final String privacyUrl = "https://www.bestkids.net/cut-video/app/generalPolicy/currencyPrivacyPolicy7.html?";
    /**
     * 隐私政策需要添加权限说明的协议链接
     */
    private static final String privacyUrl2 = "https://www.bestkids.net/cut-video/app/generalPolicy/currencyPrivacyPolicy7.html?";

    private static final String userProtocolUrl = "https://www.bestkids.net/cut-video/app/generalPolicy/userAgreement3.html?";
    private static final String vipUrl = "https://www.bestkids.net/cut-video/app/generalPolicy/purchaseInstructions.html?";

    /**
     * 跳转协议
     *
     * @param context
     * @param type
     */
    public static void start(Context context, PrivacyConstantsUtils.ProtocolType type) {
        Intent intent = new Intent(context, PrivacyWebActivity.class);
        intent.putExtra(TYPE, type.name());
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy_web);
        webView = findViewById(R.id.webView);
        myActionBar = findViewById(R.id.myActionBar);
        currentType = getIntent().getStringExtra(TYPE);
        String showPrivacyUrl = privacyUrl;
        if (PrivacyConstantsUtils.permissionDescData != null) {
            showPrivacyUrl = privacyUrl2;
            JsonObject jsonObject = new JsonObject();
            jsonObject.add("permissionDescription", new JsonParser().parse(GsonUtils.toJson(PrivacyConstantsUtils.permissionDescData)));
            webView.setData(GsonUtils.toJson(jsonObject));
        }
        String contentUrl = "";
        if (PrivacyConstantsUtils.ProtocolType.PRIVACY.name().equals(currentType)) {
            String par = String.format("appName=%s&corporateName=%s&bgColor=%s&fontColor=%s",
                    PrivacyConstantsUtils.appName,
                    PrivacyConstantsUtils.companyName,
                    PrivacyConstantsUtils.bgColor,
                    PrivacyConstantsUtils.textColor);
            contentUrl = showPrivacyUrl + par;
            myActionBar.setTitle("隐私政策");
        } else if (PrivacyConstantsUtils.ProtocolType.USER_PROTOCOL.name().equals(currentType)) {
            String par = String.format("appName=%s&corporateName=%s&bgColor=%s&fontColor=%s",
                    PrivacyConstantsUtils.appName,
                    PrivacyConstantsUtils.companyName,
                    PrivacyConstantsUtils.bgColor,
                    PrivacyConstantsUtils.textColor);
            contentUrl = userProtocolUrl + par;
            myActionBar.setTitle("用户协议");
        } else if (PrivacyConstantsUtils.ProtocolType.BUY_PROTOCOL.name().equals(currentType)) {
            String par = String.format("appName=%s&corporateName=%s&bgColor=%s&fontColor=%s&vipContent=%s&vipDescribe=%s&qq=%s",
                    PrivacyConstantsUtils.appName,
                    PrivacyConstantsUtils.companyName,
                    PrivacyConstantsUtils.bgColor,
                    PrivacyConstantsUtils.textColor,
                    PrivacyConstantsUtils.vipContent,
                    PrivacyConstantsUtils.vipDesc,
                    PrivacyConstantsUtils.qq);
            contentUrl = vipUrl + par;
            myActionBar.setTitle("购买须知");
        }
        webView.loadUrl(contentUrl);
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();
        webView = null;
    }
}